/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package docmanager.db;

import java.io.IOException;

import bf.utils.FileAccess;
import bf.utils.FileParser;

/**
 *
 * @author Juan Lucas
 */
// Por ahora, tiene todo hardcodeado.

public class ConfigData
{
	private boolean isInitialized = false;
	
//    private String configFile = System.getProperty("file.separator") + "WEB-INF" + System.getProperty("file.separator") + "config";
//	private String configFile = "C:\\config";
	private String configFile;
    private String dataBaseName = "DocManager";
    private String tempPath = System.getProperty("java.io.tmpdir");
    
    private String DBdriver;
    private String url;
    private String DBlogin;
    private String DBpassword;
    private int maxConnections;
    private String logPath;
    
    private static ConfigData instance = null;
    
    private ConfigData() {}
    
	public static ConfigData getInstance() {
		if (instance == null) {
			ConfigData.instance = new ConfigData();
		}
		return ConfigData.instance;
	}
    
    public String getTempPath() {
		return tempPath;
	}

	public void setTempPath(String tempPath) {
		this.tempPath = tempPath;
	}

	public String getLogPath() {
		if (!isInitialized) {
			init();
		}
		return logPath;
	}

	public void setLogPath(String logPath) {
		this.logPath = logPath;
	}

	public int getMaxConnections()
    {
		if (!isInitialized) {
			init();
		}
        return maxConnections;
    }

    public void setMaxConnections(int maxConnections)
    {
        this.maxConnections = maxConnections;
    }

    public String getDBdriver()
    {
    	if (!isInitialized) {
			init();
		}
        return DBdriver;
    }

    public void setDBdriver(String DBdriver)
    {
        this.DBdriver = DBdriver;
    }

    public String getDBlogin()
    {
    	if (!isInitialized) {
			init();
		}
        return DBlogin;
    }

    public void setDBlogin(String DBlogin)
    {
        this.DBlogin = DBlogin;
    }

    public String getDBpassword()
    {
    	if (!isInitialized) {
			init();
		}
        return DBpassword;
    }

    public void setDBpassword(String DBpassword)
    {
        this.DBpassword = DBpassword;
    }

    public String getDataBaseName()
    {
        return dataBaseName;
    }

    public void setDataBaseName(String dataBaseName)
    {
        this.dataBaseName = dataBaseName;
    }

    public String getUrl()
    {
    	if (!isInitialized) {
			init();
		}
        return url;
    }

    public void setUrl(String url)
    {
        this.url = url;
    }
  
    private void init() {
    	String file = "";
		try {
			file = FileAccess.getFileAsString(configFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
    	
    	DBdriver = FileParser.getValue(file, "DBdriver");
        url = FileParser.getValue(file, "url");
        DBlogin = FileParser.getValue(file, "DBlogin");
        DBpassword = FileParser.getValue(file, "DBpassword");
        maxConnections = Integer.parseInt(FileParser.getValue(file, "maxConnections"));
        logPath = FileParser.getValue(file, "logPath");
        isInitialized = true;
    }

	public void setConfigPath(String resource) {
		configFile = resource;
		System.out.println("resource es: " + resource);
	}
}
